;$(document).ready(function(){
	
	// 创建点
	function createPoint(parray,color,size) {
		var geometry = new THREE.Geometry();//声明一个空几何体对象
		for (var i = 0; i < parray.length; i++) {
			var p = new THREE.Vector3(parray[i][0],parray[i][1],parray[i][2]);
			geometry.vertices.push(p); //顶点坐标添加到geometry对象
		}
		
	    var material=new THREE.PointsMaterial({
	        color:color,
	        size:size//点对象像素尺寸
	    });//材质对象
	    return new THREE.Points(geometry,material);//点模型对象
	}
	
	// 创建线条
	function createLine (a,b,col1,col2) {
		var geometry = new THREE.Geometry();//声明一个空几何体对象
	    var p1 = new THREE.Vector3(a[0],a[1],a[2]);//顶点1坐标
	    var p2 = new THREE.Vector3(b[0],b[1],b[2]);//顶点2坐标
	    geometry.vertices.push(p1,p2); //顶点坐标添加到geometry对象
	    
	    var color1 = new THREE.Color(col1);//顶点1颜色——红色
    	var color2 = (typeof col2 == 'number') ? new THREE.Color(col2) : color1;//顶点3颜色——蓝色 
    	geometry.colors.push(color1, color2);//顶点颜色传入几何体对象
    	
	    var material=new THREE.LineBasicMaterial({
	        vertexColors: THREE.VertexColors,//以顶点颜色为准
	    });//材质对象
	    return new THREE.Line(geometry,material);//线条模型对象
	}
	
	// 创建三点面
	function createFace3(a,b,c,v,color) {
		var geometry = new THREE.Geometry(); //声明一个空几何体对象
	    var p1 = new THREE.Vector3(a[0],a[1],a[2]); //顶点1坐标
	    var p2 = new THREE.Vector3(b[0],b[1],b[2]); //顶点2坐标
	    var p3 = new THREE.Vector3(c[0],c[1],c[2]); //顶点3坐标
	    geometry.vertices.push(p1,p2,p3); //顶点坐标添加到geometry对象
	    var normal = new THREE.Vector3( v[0], v[1], v[2] ); //三角面法向量
	    var face = new THREE.Face3( 0, 1, 2, normal); //创建三角面
	    geometry.faces.push( face ); //三角面添加到几何体
	    var material=new THREE.MeshLambertMaterial({
	        color:color,//三角面颜色
	        side:THREE.DoubleSide//两面可见
	    });//材质对象
	    var mesh=new THREE.Mesh(geometry,material);//网格模型对象
	    return mesh;
	}
	
	// 创建四点面
	function createFace4(a,b,c,d,v,color) {
    	var geometry = new THREE.Geometry(); //声明一个空几何体对象
	    var p1 = new THREE.Vector3(a[0],a[1],a[2]); //顶点1坐标
	    var p2 = new THREE.Vector3(b[0],b[1],b[2]); //顶点2坐标
	    var p3 = new THREE.Vector3(c[0],c[1],c[2]); //顶点3坐标
	    var p4 = new THREE.Vector3(d[0],d[1],d[2]); //顶点4坐标
	    geometry.vertices.push(p1,p2,p3,p4); //顶点坐标添加到geometry对象
	    
	    var normal = new THREE.Vector3( v[0], v[1], v[2] ); //三角面法向量
	    var face0 = new THREE.Face3( 0, 1, 2, normal); //创建三角面0
	    var face1 = new THREE.Face3( 0, 2, 3, normal); //创建三角面1
	    geometry.faces.push( face0,face1 ); //三角面添加到几何体
	    var material=new THREE.MeshLambertMaterial({
	        color:color,//三角面颜色
	        side:THREE.DoubleSide//两面可见
	    });//材质对象
	    var mesh=new THREE.Mesh(geometry,material);//网格模型对象
	    return mesh;
    }
	
	// 创建球体
	function createSphereGeometry(r,p,c) {
		var sphere=new THREE.SphereGeometry(r,40,40);//创建一个球体几何对象
	    var sphereMaterial=new THREE.MeshLambertMaterial({color:c});//材质对象
	    var spereMesh=new THREE.Mesh(sphere,sphereMaterial);//网格模型对象
	    spereMesh.translateX(p[0]);
	    spereMesh.translateY(p[1]);
	    spereMesh.translateZ(p[2]);
		return spereMesh;
	}
	
	// 重新渲染
	function render() {
        renderer.render(scene,camera);//执行渲染操作
    }
	
	// 场景
	var scene;
	// 渲染器对象
	var renderer;
	//创建相机对象
	var camera;
	// 鼠标对象
	var mouse;
	// 射线
	var raycaster;
	
	
	function init() {
		
		// 初始化场景
		scene = new THREE.Scene();
		raycaster = new THREE.Raycaster();
		mouse =  new THREE.Vector2();
		
		// 光源设置
		var point=new THREE.PointLight(0xffffff); 
		point.position.set(400,200,300);//点光源位置
		scene.add(point);//点光源添加到场景中
		//环境光
		var ambient=new THREE.AmbientLight(0x444444);
		scene.add(ambient);
		
		// 相机设置
		var width = window.innerWidth;//窗口宽度
		var height = window.innerHeight;//窗口高度
		var k = width/height;//窗口宽高比
		var s = 100;//三维场景缩放系数
		camera=new THREE.OrthographicCamera(-s*k,s*k, s,-s,1,1000);
		camera.position.set(200,300,200);//设置相机位置
		camera.lookAt(scene.position);//设置相机方向(指向的场景对象)
		
		// 渲染器设置
		renderer=new THREE.WebGLRenderer()
		renderer.setSize(width,height);
		renderer.setClearColor(0xfcfcfc,1);//设置背景颜色
		document.body.appendChild(renderer.domElement);//body元素中插入canvas对象
		
	    var controls = new THREE.OrbitControls(camera);//创建控件对象
		controls.addEventListener('change', render);//监听鼠标、键盘事件
		
		// 创建立方体
		var box=new THREE.BoxGeometry(30,30,30);//创建一个立方体几何对象
		var material = new THREE.MeshLambertMaterial({color:0xcccccc}); //材质对象
		var mesh= new THREE.Mesh(box,material);//网格模型对象
		mesh.userData.name = '立方体';
		scene.add(mesh);//网格模型添加到场景中
		
		//圆柱体
		var box=new THREE.CylinderGeometry(0,15,30,40);
		var material = new THREE.MeshLambertMaterial({color:0xcccccc}); //材质对象
		var mesh= new THREE.Mesh(box,material);//网格模型对象
		mesh.userData.name = '圆柱体';
		mesh.translateY(30);
		scene.add(mesh);//网格模型添加到场景中
		
		// 创建球体
	    var mesh = createSphereGeometry(15,[0,60,0],0x0099CC);
	    mesh.userData.name = '圆1';
	    scene.add(mesh);//网格模型添加到场景中
	    
		// 创建线条对象
	    var line1 = createLine([-100,0,0],[100,0,0],0x0000ff);
	    var line2 = createLine([0,-100,0],[0,100,0],0x00ff00);
	    var line3 = createLine([0,0,-100],[0,0,100],0xff0000);
	    scene.add(line1);//线条对象添加到场景中
	    scene.add(line2);//线条对象添加到场景中
	    scene.add(line3);//线条对象添加到场景中
	    
	    // 创建面
	    //var face1 = createFace3([0,0,0],[80,0,0],[0,80,0],[0,0,1],0x6c88d2);
	    //var face2 = createFace3([0,0,0],[0,80,0],[0,0,80],[0,0,1],0x6c88d2);
	    //var face3 = createFace3([0,0,0],[80,0,0],[0,0,80],[0,0,1],0x6c88d2);
	    //scene.add(face1);//网格模型添加到场景中
	    //scene.add(face2);//网格模型添加到场景中
	    //scene.add(face3);//网格模型添加到场景中
	    
	    // 创建点对象
	    var points = createPoint([[100,0,0],[0,100,0],[0,0,100]],0x000000,10.0);
	    scene.add(points);//点对象添加到场景中
		
		// 创建矩形平面网格模型
	    var face4 = createFace4([0,0,-80],[80,0,-80],[80,80,-80],[0,80,-80],[0,0,1],0x99CCFF);
	    face4.userData.name = '平面';
	    scene.add(face4);//网格模型添加到场景中
	    
	    // 创建弧线
	    var shape = new THREE.Shape();
	    shape.absarc(0,0,100, 0, 0.5*Math.PI);
	    var geometry = new THREE.ShapeGeometry(shape);
	    var material=new THREE.LineBasicMaterial({color:0x0000ff});//材质对象
	    var line=new THREE.Line(geometry,material);//线模型对象
	    scene.add(line);//线模型添加到场景中
	    
	    /**
	     * 创建管道网格模型
	     */
	    var path = new THREE.SplineCurve3([//创建轮廓的扫描轨迹(3D样条曲线)
	        new THREE.Vector3( -80, 0, 0 ),
	        new THREE.Vector3( -80, 5, 20 ),
	        new THREE.Vector3( -80, 10, 50 ),
	        new THREE.Vector3( -80, 20, 50 ),
	        new THREE.Vector3( -80, 40, 0)
	    ]);
	    var geometry = new THREE.TubeGeometry( path, 40, 2, 8, false );
	    var material=new THREE.MeshPhongMaterial({
	        color:0xFF9999,
	        side:THREE.DoubleSide//两面可见
	    });//材质对象
	    var mesh=new THREE.Mesh(geometry,material);//管道网格模型对象
	    mesh.userData.name = '管道';
	    scene.add(mesh);//管道网格模型添加到场景中
	    
	    // 创建字体
		var loader = new THREE.FontLoader();//新建字体对象
		var fontJsonUrl = 'plugin/threejs/fonts/FZLanTingHeiS-UL-GB_Regular.json'; //你的字体json路径
		loader.load( fontJsonUrl, function ( response ) {
			//注意response 这内容比较关键，是否能加载中文就看这个变量了，这个变量里面的glyphs，默认的几种只有英文
		    var options = {
	          	size: 8, //字号大小，一般为大写字母的高度
	          	height: 2, //文字的厚度
	          	weight: 'bold', //值为'normal'或'bold'，表示是否加粗
	          	font: response, //字体，默认是'helvetiker'，需对应引用的字体文件
	          	style: 'normal', //值为'normal'或'italics'，表示是否斜体
	          	bevelThickness: 1, //倒角厚度
	          	bevelSize: 1, //倒角宽度
	          	curveSegments: 50,//弧线分段数，使得文字的曲线更加光滑
	          	bevelEnabled: true //布尔值，是否使用倒角，意为在边缘处斜切
	        };
		    //创建一个三维对象的文本作为一个单一的对象 text输入的文字 options 文字设置
		    var textGeo = new THREE.TextGeometry( '我是文字哈哈', options);
		    textGeo.computeBoundingBox();
		    textGeo.computeVertexNormals();
		
		    var material = new THREE.MultiMaterial([
		        new THREE.MeshPhongMaterial( { color: 0x0} )
		    ]);
		    //新建mesh,加入
		    mesh = new THREE.Mesh( textGeo, material );
		    mesh.position.x = 20;
		    mesh.position.y = 0;
		    mesh.position.z = 0;
		    mesh.rotation.x = 0;
		    mesh.rotation.y = Math.PI * 2;
		    scene.add( mesh );
		    
		    render();
		});
	    
	    //执行渲染操作
		render();
		
		initBind();
	}
	
	// 绑定事件
	function initBind() {
		// 鼠标点击
		document.addEventListener( 'mousedown', function(){
			event.preventDefault();
			mouse.x = ( event.clientX / renderer.domElement.clientWidth ) * 2 - 1;
			mouse.y = - ( event.clientY / renderer.domElement.clientHeight ) * 2 + 1;
			raycaster.setFromCamera( mouse, camera );
			var intersects = raycaster.intersectObjects( scene.children );
			if ( intersects.length > 0 ) {

				var firstD = intersects[ 0 ];
				var obj = firstD.object;
				if (obj.name != 'showPoint' && obj.name != 'showLine') {
					
					obj.material.color.setHex( Math.random() * 0xffffff );
					$("#tipdiv").html('当前对象：'+ obj.userData.name);
					
					//移除之前的线和端点
					var oldP = scene.getChildByName('showPoint');
					scene.remove(oldP);
					var oldL = scene.getChildByName('showLine');
					scene.remove(oldL);
					
					// 计算两个端点
					var x = firstD.point.x;
					var y = firstD.point.y;
					var z = firstD.point.z;
					var r = 40;
					var p1 = [x,y,z];
					var p2 = [x<0?x-r:x+r,y<0?y-r:y+r,z<0?z-r:z+r];
					
					// 端点
					var spereMesh = createSphereGeometry(4,p2,0x0099CC);
					spereMesh.name = 'showPoint';
					spereMesh.userData.info = obj.userData.name;
		    		scene.add(spereMesh);
		    		
		    		// 线
		    		var line = createLine(p1,p2,0xff0000);
		    		line.name = 'showLine';
		    		scene.add(line);
					
					render();
				} else if (obj.name == 'showPoint') {
					if (confirm('是否打开'+obj.userData.info+'的详细数据？')) {
						window.location.href = 'http://www.baidu.com';
					}
				}
				
			}
			
		}, false );
	}
	
	init();
	
});
